package com.xingji.caixia.entitys;

import com.xingji.caixia.MyApplication;

import org.xutils.db.sqlite.WhereBuilder;
import org.xutils.ex.DbException;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zyh on 2017/9/23.
 */

public class CardDao {

    /**
     * 返回打卡信息
     * @param type
     * @return
     * @throws DbException
     */
    public static List<UploadData> getCards(dataType type) throws DbException {
        List<UploadData> list = null;
        switch (type){
            case ALL:
                list =  MyApplication.db.selector(UploadData.class).orderBy("id",true).findAll();
                break;
            case UPLOADED:
                WhereBuilder whereBuilder = WhereBuilder.b("isupload","=",1);
                list =  MyApplication.db.selector(UploadData.class).where(whereBuilder).orderBy("id",true).findAll();
                break;
            case UNPLOAD:
                WhereBuilder whereBuilder1 = WhereBuilder.b("isupload","=",0);
                list = MyApplication.db.selector(UploadData.class).where(whereBuilder1).orderBy("id",true).findAll();
                break;
        }
        return (list!=null)?list:new ArrayList<UploadData>();
    }


    public static UploadData getLast() throws DbException {
        return MyApplication.db.selector(UploadData.class).orderBy("id",true).findFirst();
    }

    /**
     * 添加新卡记录
     * @param data
     * @throws DbException
     */
    public static void addNewData(UploadData data) throws DbException {
        MyApplication.db.save(data);
    }

    /**
     * 更新卡片记录
     * @param data
     * @throws DbException
     */
    public static void update(UploadData data) throws DbException {
        MyApplication.db.update(data,"isupload","result","issad");
    }

    public static void deleteCache(UploadData data) throws DbException {
        MyApplication.db.delete(data);
    }

}

